package com.education.wisdom.controller.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.pojo.SysUserOther;
import com.education.common.utils.DateLiuUtil;
import com.education.common.utils.LogUtil;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.*;
import com.education.wisdom.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.education.common.core.BaseController.writeJSON;


/**
 * 目录体系 信息操作处理
 *
 * @author ruoyi
 * @date 2018-11-15
 */
@Controller
@CrossOrigin
@Slf4j
@ResponseBody
@Api(tags = {"专业课程体系目录"})
@RequestMapping("/appFullCatalog")
public class AppFullCatalogController {

    @Autowired
    private ICatalogService catalogService;
    @Autowired
    private IPaperService paperService;
    @Autowired
    private IUserCatalogBuyService userCatalogBuyService;
    @Autowired
    private IResourcesService resourcesService;
    @Autowired
    private ICommentsService commentsService;
    @Autowired
    private ILabelService labelService;
    @Autowired
    private IUserAccountService accountService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private ICurrencyRecordService recordService;

    @GetMapping("/catalogList")
    @ApiOperation("查询专业大类体系列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "登录用户id", dataType = "String")})
    public void catalogList(HttpServletResponse response, String userId) throws Exception {
        log.info(LogUtil.LogInfo("查询专业大类体系列表", new Date()));
        //SysAppLogin sysAppLogin = loginService.selectSysAppLoginById(userId);
        List<Catalog> catalogList = catalogService.selectByThreeCatalogId();
        for (Catalog catalogs : catalogList) {
            for (Catalog catalog : catalogs.getCatalogs()) {
                for (Catalog catalog1 : catalog.getCatalogs()) {
                    //登录
                    //1普通用户
                    //if (sysAppLogin.getCustomerType().equals("1")) {
                    Map<String, Object> hashmap = new HashMap<>();
                    hashmap.put("userId", userId);
                    hashmap.put("catalogId", catalog1.getCatalogId());
                    UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
                    //JrOrder jrOrder = orderService.selectJrOrderByUserId(hashmap);
                    if (userCatalogBuy == null) {
                        catalog1.setIsRed("0");
                        catalog1.setCatalogBanner(null);
                    } else {
                        catalog1.setIsRed("1");
                    }
                    //} else {
                    //    //2线下分配用户
                    //    Map<String, Object> map2 = new HashMap<>();
                    //    map2.put("schoolId", sysAppLogin.getSchoolId());
                    //    map2.put("userType", "2");
                    //    SysAppLogin login = loginService.selectByDeptId(map2);
                    //    Map<String, Object> hashmap = new HashMap<>();
                    //    hashmap.put("userId", login.getUserId());
                    //    hashmap.put("catalogId", catalog1.getCatalogId());
                    //    UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
                    //    JrOrder jrOrder = orderService.selectJrOrderByUserId(hashmap);
                    //    if (jrOrder == null) {
                    //        catalog1.setIsRed("0");
                    //    } else {
                    //        catalog1.setIsRed("1");
                    //    }
                    //    Map<String, Object> objectMap = new HashMap<>();
                    //    objectMap.put("userId", userId);
                    //    objectMap.put("catalogId", catalog1.getCatalogId());
                    //    UserCatalogBuy catalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(objectMap);
                    //    JrOrder jrOrder1 = orderService.selectJrOrderByUserId(hashmap);
                    //    if (jrOrder1 != null) {
                    //        catalog1.setIsRed("1");
                    //    }
                    //}
                }
            }
        }
        writeJSON(catalogList, response, "200", "成功");
    }

    @GetMapping("/newList")
    @ResponseBody
    @ApiOperation("查询最新课程")
    public void newList(HttpServletResponse response, String userId, Integer count, Integer pageNo) throws Exception {
        log.info(LogUtil.LogInfo("查询最新课程", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        //登录
        //1普通用户
        if (count == null) {
            PageHelper.startPage(pageNo, 10);
        }
        List<Catalog> list = catalogService.newClass(map);
        for (Catalog catalog : list) {
            Map<String, Object> hashmap = new HashMap<>();
            hashmap.put("userId", userId);
            hashmap.put("catalogId", catalog.getCatalogId());
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
            } else {
                catalog.setIsRed("1");
            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @GetMapping("/hotClass")
    @ApiOperation("查询热门课程")
    public void hotClass(HttpServletResponse response, String userId, Integer count, Integer pageNo) throws Exception {
        log.info(LogUtil.LogInfo("查询热门课程", new Date()));
        //SysAppLogin appLogin = loginService.selectSysAppLoginById(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        //登录
        //1普通用户
        //if (appLogin.getCustomerType().equals("1")) {
        if (count == null) {
            PageHelper.startPage(pageNo, 10);
        }
        List<Catalog> list = catalogService.hotClass(map);
        for (Catalog catalog : list) {
            Map<String, Object> hashmap = new HashMap<>();
            hashmap.put("userId", userId);
            hashmap.put("catalogId", catalog.getCatalogId());
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
            //JrOrder jrOrder = orderService.selectJrOrderByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
            } else {
                catalog.setIsRed("1");
            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
        /*} else {
            2线下分配用户
            Map<String, Object> map2 = new HashMap<>();
            map2.put("schoolId", appLogin.getSchoolId());
            map2.put("userType", "2");
            SysAppLogin login = loginService.selectByDeptId(map2);
            if (count == null) {
                PageHelper.startPage(pageNo, 10);
            }
            List<Catalog> list = catalogService.hotClass(map);
            for (Catalog catalog : list) {
                Map<String, Object> hashmap = new HashMap<>();
                hashmap.put("userId", login.getUserId());
                hashmap.put("catalogId", catalog.getCatalogId());
                UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
                JrOrder jrOrder = orderService.selectJrOrderByUserId(hashmap);
                if (jrOrder == null) {
                    catalog.setIsRed("0");
                } else {
                    catalog.setIsRed("1");
                }
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("userId", userId);
                objectMap.put("catalogId", catalog.getCatalogId());
                UserCatalogBuy catalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(objectMap);
                JrOrder jrOrder1 = orderService.selectJrOrderByUserId(hashmap);
                if (jrOrder1 != null) {
                    catalog.setIsRed("1");
            }
            writeJSON(new PageInfo<>(list), response, "200", "成功");
        }*/
    }

    @GetMapping("/userLike")
    @ResponseBody
    @ApiOperation("猜你喜欢列表展示")
    @ApiImplicitParam(name = "userId", value = "用户编号", dataType = "String")
    public void userLike(HttpServletResponse response, String userId, Integer count, Integer pageSize, Integer pageNo) throws Exception {
        log.info(LogUtil.LogInfo("猜你喜欢列表展示", new Date()));
        //SysAppLogin appLogin = loginService.selectSysAppLoginById(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        //登录
        //1普通用户
        //if (appLogin.getCustomerType().equals("1")) {
        if (count == null) {
            PageHelper.startPage(pageNo, 10);
        }
        List<Catalog> list = catalogService.selectList(map);
        for (Catalog catalog : list) {
            Map<String, Object> hashmap = new HashMap<>();
            hashmap.put("userId", userId);
            hashmap.put("catalogId", catalog.getCatalogId());
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
            //JrOrder jrOrder = orderService.selectJrOrderByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
            } else {
                catalog.setIsRed("1");
            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
        //} else {
        //2线下分配用户
        //    Map<String, Object> map2 = new HashMap<>();
        //    map2.put("schoolId", appLogin.getSchoolId());
        //    map2.put("userType", "2");
        //    SysAppLogin login = loginService.selectByDeptId(map2);
        //    if (count == null) {
        //        PageHelper.startPage(pageNo, 10);
        //    }
        //    List<Catalog> list = catalogService.selectList(map);
        //    for (Catalog catalog : list) {
        //        Map<String, Object> hashmap = new HashMap<>();
        //        hashmap.put("userId", login.getUserId());
        //        hashmap.put("catalogId", catalog.getCatalogId());
        //        UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
        //        JrOrder jrOrder = orderService.selectJrOrderByUserId(hashmap);
        //        if (jrOrder == null) {
        //            catalog.setIsRed("0");
        //        } else {
        //            catalog.setIsRed("1");
        //        }
        //        Map<String, Object> objectMap = new HashMap<>();
        //        objectMap.put("userId", userId);
        //        objectMap.put("catalogId", catalog.getCatalogId());
        //        UserCatalogBuy catalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(objectMap);
        //        JrOrder jrOrder1 = orderService.selectJrOrderByUserId(hashmap);
        //        if (jrOrder1 != null) {
        //            catalog.setIsRed("1");
        //        }
        //    }
        //    writeJSON(new PageInfo<>(list), response, "200", "成功");
        //}
    }


    @GetMapping("/threeLevelDirectory")
    @ApiOperation("查询专业大类3级目录")
    @ApiImplicitParams({@ApiImplicitParam(name = "catalogId", value = "课程ID", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "登录用户id", dataType = "String")})
    public void threeLevelDirectory(HttpServletResponse response, String userId, Integer catalogId) throws Exception {
        log.info(LogUtil.LogInfo("查询专业大类3级目录", new Date()));
//        SysAppLogin sysAppLogin = loginService.selectSysAppLoginById(userId);
        Integer id = userFeign.selectOneUserTwo(userId);
//        Map<String, Object> map2 = new HashMap<>();
//        map2.put("schoolId", );
//        map2.put("userType", "2");
//        SysAppLogin appLogin = loginService.selectByDeptId(map2);
        Map<String, Object> map = new HashMap<>();
        map.put("catalogId", catalogId);
        map.put("userId", id);
        UserCatalogBuy userCatalog = userCatalogBuyService.selectUserCatalogBuyByUserId(map);
//        JrOrder jrOrder = orderService.selectJrOrderByUserId(map);
        //大类必须买了才能进资源查看
        if (userCatalog != null) {
            List<Catalog> catalogs = catalogService.selectByParentId(catalogId);
            for (Catalog catalog : catalogs) {
                catalog.setIsRed("1");
            }
            writeJSON(catalogs, response, "200", "success");
        } else {
            writeJSON("未购买目录", response, "200", "success");
        }
    }

    @GetMapping("/threeDetails")
    @ApiOperation("查询3级资源详情")
    public void threeDetails(HttpServletResponse response, String userId, Integer catalogId) throws IOException {
        log.info(LogUtil.LogInfo("查询3级资源详情", new Date()));
        Catalog catalog = catalogService.selectCatalogByCatalogId(catalogId);
        Integer id = userFeign.selectOneUserTwo(userId);
//        Map<String, Object> map2 = new HashMap<>();
//        map2.put("schoolId", sysAppLogin.getSchoolId());
//        map2.put("userType", "2");
//        SysAppLogin appLogin = loginService.selectByDeptId(map2);
        Map<String, Object> map = new HashMap<>();
        map.put("catalogId", catalog.getParentId());
        map.put("userId", id);
        UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(map);
//        JrOrder jrOrder = orderService.selectJrOrderByUserId(map);
        if (userCatalogBuy != null) {
            writeJSON(catalog, response, "200", "success");
            if (catalog.getCatalogLearningNum() == null) {
                catalog.setCatalogLearningNum(1);
            } else {
                catalog.setCatalogLearningNum(catalog.getCatalogLearningNum() + 1);
            }
            catalogService.updateCataLog(catalog);
        } else {
            writeJSON("非法", response, "200", "success");
        }
    }

    /**
     * 查询专业课程详情
     *
     * @param catalogId
     * @param userId
     * @param response
     * @throws Exception
     */
    @GetMapping("/info")
    @ApiOperation("查询课程详情")
    public void classInfo(Integer catalogId, Integer userId, HttpServletResponse response) throws Exception {
        log.info(LogUtil.LogInfo("查询课程详情", new Date()));
        Catalog catalog = catalogService.classInfo(catalogId);
        if (catalog.getCatalogLearningNum() == null) {
            catalog.setCatalogLearningNum(1);
        } else {
            catalog.setCatalogLearningNum(catalog.getCatalogLearningNum() + 1);
        }
        catalogService.updateCataLog(catalog);
        if (userId != null) {
            Map<String, Object> hashmap = new HashMap<>();
            hashmap.put("userId", userId);
            hashmap.put("catalogId", catalogId);
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
            } else {
                catalog.setIsRed("1");
            }
        }
        writeJSON(catalog, response, "200", "success");
    }

    @GetMapping("/type")
    @ApiOperation("查询素材课件信息")
    public void selectByType(HttpServletResponse response, String catalogId, String type, Integer pageNum, Integer pageSize) throws IOException {
        log.info(LogUtil.LogInfo("查询素材课件信息", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("rCatalogid", catalogId);
        map.put("type", type);
        PageHelper.startPage(pageNum, pageSize);
        List<Resources> list = resourcesService.selectInfoByType(map);
        PageInfo<Resources> page = new PageInfo<>(list);
        writeJSON(page, response, "200", "success");
    }

    @GetMapping("/virtualTeaching")
    public void virtualTeaching(HttpServletResponse response, Integer catalogId, Integer pageNum, Integer pageSize) throws IOException {
        log.info(LogUtil.LogInfo("查询章节信息", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<Catalog> catalogs = catalogService.selectByParentId(catalogId);
        for (Catalog catalog : catalogs) {
            Map<String, Object> map = new HashMap<>();
            map.put("rCatalogid", catalog.getCatalogId());
            List<Resources> resources = resourcesService.selectVideoById(map);
            for (Resources resource : resources) {
                if (resource.getType().equals("9")) {
                    catalog.setFlash(resource.getrUrl());
                } else if (resource.getType().equals("8")) {
                    catalog.setThreeD(resource.getrUrl());
                }
            }
        }
        writeJSON(new PageInfo<>(catalogs), response, "200", "success");
    }

    @PostMapping("/addSave")
    @ApiOperation("发布课后评价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "plText", value = "评论内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plCreateuser", value = "发布人", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plCurriculumid", value = "课程编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plImage", value = "评论图片", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plAttribute", value = "属性标签编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plPraise", value = "1：好评，2：中评，3：差评", required = true, dataType = "String")})
    public void addSave(HttpServletResponse response, Comments comments) throws Exception {
        log.info(LogUtil.LogInfo("发布课后评价", new Date()));
        comments.setPlId(UUIDUtils.getByUUId());
        comments.setPlCreatetime(new Date());
        commentsService.insertComments(comments);
        writeJSON("添加成功", response, "200", "成功");
    }

    @GetMapping("/allEvaluation")
    @ApiOperation("查询全部评价")
    public void list(HttpServletResponse response, Integer catalogId) throws IOException {
        log.info(LogUtil.LogInfo("查询全部评价", new Date()));
        List<Comments> comments = commentsService.selectByCatalogId(catalogId);
        //好评
        Integer good = 0;
        //中评
        Integer average = 0;
        //差评
        Integer badReview = 0;
        for (Comments comment : comments) {
            if (comment.getPlPraise().equals("1")) {
                good++;
            } else if (comment.getPlPraise().equals("2")) {
                average++;
            } else if (comment.getPlPraise().equals("3")) {
                badReview++;
            }
//            SysAppLogin sysAppLogin = loginService.selectSysAppLoginById(comment.getPlCreateuser());
            SysUserOther sysUserOther = userFeign.selectOnePCF(comment.getPlCreateuser());
            sysUserOther.setSysPassword(null);
            comment.setUserOther(sysUserOther);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("good", good);
        map.put("average", average);
        map.put("badReview", badReview);
        writeJSON(map, response, "200", "成功");
    }

    @GetMapping("/screeningEvaluation")
    @ApiOperation("筛选好，中，差评价")
    public void screeningEvaluation(HttpServletResponse response, Integer catalogId, String type, Integer pageNo) throws IOException {
        log.info(LogUtil.LogInfo("筛选好，中，差评价", new Date()));
        int pageSize = 10;
        Map<String, Object> map = new HashMap<>();
        map.put("catalogId", catalogId);
        map.put("type", type);
        PageHelper.startPage(pageNo, pageSize);
        List<Comments> comments = commentsService.selectByType(map);
        for (Comments comment : comments) {
            SysUserOther sysUserOther = userFeign.selectOnePCF(comment.getPlCreateuser());
//            SysAppLogin sysAppLogin = loginService.selectSysAppLoginById(comment.getPlCreateuser());
            comment.setUserName(sysUserOther.getSysRelname());
            comment.setUserIcoUrl(sysUserOther.getSysIco());
            Label label = labelService.selectLabelById(comment.getPlAttribute());
            if (label != null) {
                comment.setLabelName(label.getBqLabel());
            }
        }
        writeJSON(new PageInfo<>(comments), response, "200", "成功");
    }

    @GetMapping("/labelScreening")
    @ApiOperation("按标签筛选评价")
    public void labelScreening(HttpServletResponse response, Integer catalogId, String labeId, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("按标签筛选评价", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("catalogId", catalogId);
        map.put("labeId", labeId);
        PageHelper.startPage(pageNum, pageSize);
        List<Comments> comments = commentsService.selectByLabeId(map);
        for (Comments comment : comments) {
            SysUserOther sysUserOther = userFeign.selectOnePCF(comment.getPlCreateuser());
            sysUserOther.setSysPassword(null);
            comment.setUserOther(sysUserOther);
//            SysAppLogin sysAppLogin = loginService.selectSysAppLoginById(comment.getPlCreateuser());
//            sysAppLogin.setUserPassword(null);
//            comment.setSysAppLogin(sysAppLogin);
        }
        writeJSON(new PageInfo<>(comments), response, "200", "成功");
    }

    /**
     * 查询试卷列表
     */
    @GetMapping("/paperList")
    @ApiOperation("查询专业课程详情下的试卷")
    @ApiImplicitParams({@ApiImplicitParam(name = "catalogId", value = "参数集合", dataType = "String"),})
    public void paperlist(HttpServletResponse response, String catalogId) throws Exception {
        log.info(LogUtil.LogInfo("查询专业课程详情下的试卷", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("catalogId", catalogId);
        List<Paper> list = paperService.selectPaperListById(map);
        writeJSON(list, response, "200", "成功");
    }


    /**
     * 订单号生成   20200718112629527
     */
    public static String nextItemNo() {
        LocalDateTime dateTime = LocalDateTime.now();
        return DateLiuUtil.getOtherFormatterLocalDateTime(dateTime);
    }

    /**
     * 学生APP购买专业课程资源
     *
     * @param response
     * @param catalogBuy
     * @throws IOException
     */
    @PostMapping("/addBatch")
    @ApiOperation("新增购买目录")
    @Transactional(rollbackFor = Exception.class)
    public void addBatch(HttpServletResponse response, UserCatalogBuy catalogBuy) throws IOException {
        //获取课程
        Catalog catalog = catalogService.selectCatalogByCatalogId(catalogBuy.getCatalogId());
        //获取用户账户表
        SysUserOther sysUserOther = userFeign.selectOnePCF(catalogBuy.getUserId());
        //UserAccount userAccount = accountService.selectUserAccountByUserId(catalogBuy.getUserId());
        //获取到达人币
        BigDecimal currency = sysUserOther.getPrice();
        //获取课程价格
        Integer price = catalog.getPrice();
        Map<String, Object> map = new HashMap<>();
        map.put("userId", catalogBuy.getUserId());
        map.put("catalogId", catalogBuy.getCatalogId());
        UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(map);
        if (userCatalogBuy == null) {
            //if (currency >= price) {
            if (currency.compareTo(new BigDecimal(price)) >= 0) {
                //UserAccount uaccount = new UserAccount();
                //uaccount.setCurrency(currency - price);
                //uaccount.setUserId(catalogBuy.getUserId());
                //accountService.updateUserAccount(uaccount);
                Map<String, Object> mp = new HashMap<>();
                mp.put("id", catalogBuy.getUserId());
                //mp.put("price", currency - price);
                mp.put("price", currency.subtract(new BigDecimal(price)));
                //userFeign.updataByUserId(mp);
                userFeign.updatePriceByUserId(Integer.parseInt(catalogBuy.getUserId()), currency.subtract(new BigDecimal(price)));
                catalogBuy.setItFrom(1);
                userCatalogBuyService.insert(catalogBuy);
                //jr_currency_record 课程购买交易记录表
                CurrencyRecord currencyRecord = new CurrencyRecord();
                //课程id
                currencyRecord.setCatalogId(catalogBuy.getCatalogId());
                //类型 资源购买
                currencyRecord.setTrxType("3");
                //价格
                currencyRecord.setCurrency(new BigDecimal(price));
                //创建时间
                currencyRecord.setCreateTime(new Date());
                //付款时间
                currencyRecord.setEditTime(new Date());
                //订单id
                currencyRecord.setCurrencyId(nextItemNo());
                //status
                currencyRecord.setStatus("1");
                //remark
                currencyRecord.setRemark("专业课程购买");
                //userId
                currencyRecord.setUserId(catalogBuy.getUserId());
                //支付者姓名
                currencyRecord.setPayerName(sysUserOther.getSysRelname());
                //删除标志
                currencyRecord.setDelFlag("0");
                currencyRecord.setOrderType("1");
                recordService.insertCurrencyRecord(currencyRecord);
                //添加购买消息
                Message message = new Message();
                message.setIsRead("1");
                message.setUserId(Integer.parseInt(catalogBuy.getUserId()));
                message.setType("1");
                message.setCreatetime(new Date());
                message.setTitle("您已购买课程 : " + catalog.getName());
                messageService.insertMessage(message);
                writeJSON("购买成功", response, "200", "success");
            } else {
                writeJSON("账户余额不足", response, "300", "fail");
            }
        } else {
            writeJSON("已经购买", response, "300", "fail");
        }
    }
}
